1.4 Control Statements in C
Module 1.4 • Execution Mappings, Branching & Loop Structures
1.4.1 Introduction
By default, a C program executes statements one after another from top to bottom. However, many real-world problems require decisions, repetition, or skipping certain instructions.
Control statements allow us to change the normal flow of execution according to program requirements.
Using control statements, we can:
- Execute code only when a condition is true.
- Repeat a set of statements multiple times.
- Skip certain instructions.
- Exit from loops when needed.
- Transfer control to another part of the program.
Control statements are mainly divided into three categories:
- Decision Making Statements
- Looping Statements
- Jump Statements
1.4.2 Decision Making Statements
Decision-making statements allow a program to choose different actions based on conditions.
A. if Statement
The if statement executes a block of code only when the specified condition is true.
Syntax
if(condition)
{
statements;
}
Example
#include <stdio.h>
int main()
{
int marks = 78;
if(marks >= 50)
{
printf("Pass");
}
return 0;
}
Output
Pass
B. if...else Statement
The if...else statement provides two possible paths.
Syntax
if(condition)
{
statements;
}
else
{
statements;
}
Example
#include <stdio.h>
int main()
{
int age = 16;
if(age >= 18)
{
printf("Eligible to Vote");
}
else
{
printf("Not Eligible to Vote");
}
return 0;
}
Output
Not Eligible to Vote
C. else-if Ladder
Used when multiple conditions need to be checked.
Example
#include <stdio.h>
int main()
{
int score = 82;
if(score >= 90)
printf("Grade A");
else if(score >= 75)
printf("Grade B");
else if(score >= 60)
printf("Grade C");
else
printf("Grade D");
return 0;
}
Output
Grade B
Important Note
The moment one condition becomes true, the remaining conditions are skipped.
D. Nested if Statement
An if statement can be placed inside another if statement.
Example
#include <stdio.h>
int main()
{
int age = 24;
int salary = 35000;
if(age >= 18)
{
if(salary >= 30000)
printf("Loan Approved");
}
return 0;
}
Output
Loan Approved
E. switch Statement
The switch statement is useful when a variable can have many possible values.
Syntax
switch(expression)
{
case value1:
statements;
break;
case value2:
statements;
break;
default:
statements;
}
Example
#include <stdio.h>
int main()
{
int day = 4;
switch(day)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
default:
printf("Invalid Day");
}
return 0;
}
Output
Thursday
1.4.3 Looping Statements
Loops are used when a set of statements must be executed repeatedly. C provides three loops:
- while
- do...while
- for
A. while Loop
The condition is checked before executing the loop body.
Syntax
while(condition)
{
statements;
}
Example
#include <stdio.h>
int main()
{
int n = 1;
while(n <= 5)
{
printf("%d ", n);
n++;
}
return 0;
}
Output
1 2 3 4 5
B. do...while Loop
The loop body executes first and the condition is checked afterward.
Syntax
do
{
statements;
}
while(condition);
Example
#include <stdio.h>
int main()
{
int n = 1;
do
{
printf("%d ", n);
n++;
}
while(n <= 5);
return 0;
}
Output
1 2 3 4 5
Important Feature
A do...while loop always executes at least one time.
C. for Loop
Used when the number of repetitions is known.
Syntax
for(initialization; condition; update)
{
statements;
}
Example
#include <stdio.h>
int main()
{
int i;
for(i = 1; i <= 5; i++)
{
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5
1.4.4 Nested Loops
A loop inside another loop is called a nested loop.
Example
#include <stdio.h>
int main()
{
int i, j;
for(i = 1; i <= 3; i++)
{
for(j = 1; j <= 4; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
Output
* * * * * * * * * * * *
1.4.5 Infinite Loops
A loop that never stops executing is called an infinite loop.
Example
while(1)
{
printf("Running...");
}
Since the condition is always true, the loop continues forever.
Common Causes
- Forgetting to update loop variables.
- Wrong loop condition.
- Using assignment operator (=) instead of equality operator (==).
Example:
while(value = 1)
{
}
This becomes an infinite loop because value = 1 always produces a true value.
1.4.6 break Statement
The break statement immediately terminates a loop or switch block.
Example
#include <stdio.h>
int main()
{
int i;
for(i = 1; i <= 10; i++)
{
if(i == 6)
break;
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5
1.4.7 continue Statement
The continue statement skips the remaining statements of the current iteration and moves to the next iteration.
Example
#include <stdio.h>
int main()
{
int i;
for(i = 1; i <= 5; i++)
{
if(i == 3)
continue;
printf("%d ", i);
}
return 0;
}
Output
1 2 4 5
1.4.8 goto Statement
The goto statement transfers program control to a labeled statement.
Syntax
goto label;
label:
statement;
Example
#include <stdio.h>
int main()
{
int n = 1;
start:
printf("%d ", n);
n++;
if(n <= 4)
goto start;
return 0;
}
Output
1 2 3 4
Note
Although valid, excessive use of goto can make programs difficult to understand and maintain.
1.4.9 Compound Statement (Block)
A group of statements enclosed within curly braces { } is called a block or compound statement.
Example
{
int length = 10;
int width = 5;
printf("%d", length * width);
}
All statements inside the braces are treated as a single unit.
1.4.10 Comparison of Loops
| Feature | while | do...while | for |
|---|---|---|---|
| Condition Checked | Before Loop | After Loop | Before Loop |
| Executes At Least Once | No | Yes | No |
| Best Used When | Iterations Unknown | Must Run Once | Iterations Known |
1.4.11 Real-Life Uses of Control Statements
Decision Making
- ATM menu selection
- Exam result processing
- Login verification
Loops
- Printing reports
- Processing records
- Generating tables
Jump Statements
- Exiting menus
- Skipping invalid data
- Terminating loops early
Summary
- Control statements manage the flow of execution in a C program.
- Decision statements include if, if...else, else-if, and switch.
- Loop statements include while, do...while, and for.
- break exits a loop immediately.
- continue skips the current iteration.
- goto transfers control to a labeled statement.
- Nested loops and nested if statements allow complex program logic.
- Proper use of control statements makes programs efficient and easy to understand.
Click your choice for each question to view feedback immediately. Complete all questions to evaluate your metric score.